package com.nulldev.util.internal.backport.httpclient_rw.httpclient;

import java.io.InputStream;
import java.io.Reader;

import com.nulldev.util.web.HttpClient.CachingRequestType;
import com.nulldev.util.web.HttpClient.HttpClient;
import com.nulldev.util.web.HttpClient.HttpRequest;
import com.nulldev.util.web.HttpClient.ResponseFormat;
import com.nulldev.util.web.HttpClient.providers.HttpClientProvider;
import com.nulldev.util.web.HttpClient.providers.websockets.HttpWebSocketRequest;

public class BackportProvider implements HttpClientProvider {

	@Override
	public HttpRequest<String> getString() {
		return new BackportedHttpRequest<String>() {

			@Override
			public ResponseFormat getFormat() {
				return HttpClient.DISABLE_STRING_STREAM_METHOD ? ResponseFormat.STRING : ResponseFormat.STRING_VIA_STREAM;
			}
		};
	}

	@Override
	public HttpRequest<byte[]> getBytes() {
		return new BackportedHttpRequest<byte[]>() {

			@Override
			public ResponseFormat getFormat() {
				return ResponseFormat.BYTES;
			}
		};
	}

	@Override
	public HttpRequest<InputStream> getStream() {
		return new BackportedHttpRequest<InputStream>() {

			@Override
			public ResponseFormat getFormat() {
				return ResponseFormat.STREAM;
			}
		};
	}

	@Override
	public HttpRequest<Reader> getReader() {
		return new BackportedHttpRequest<Reader>() {

			@Override
			public ResponseFormat getFormat() {
				return ResponseFormat.READER;
			}
		};
	}

	@Override
	public boolean isLegacyCompatible() {
		return false;
	}

	@Override
	public String getID() {
		return "nullUtil.backport";
	}

	@Override
	public boolean providesRequests() {
		return true;
	}

	@Override
	public boolean providesWebSockets() {
		return true;
	}

	@Override
	public CachingRequestType defaultCachingType() {
		return CachingRequestType.CACHE_IF_POSSIBLE;
	}

	@Override
	public HttpWebSocketRequest getSocket() {
		return new BackportedWebSocketRequest();
	}

	private static final ProviderInfo[] PV = new ProviderInfo[]
		{ ProviderInfo.WILL_RUN_ON_CT };

	@Override
	public ProviderInfo[] providerInfo() {
		return PV;
	}
}
